package evolution.operation.selection;

import evolution.individual.Individual;
import evolution.random.RandomGenerator;

public class TournamentSelection implements SelectionStrategy {

	private int tournamentSize;
	private RandomGenerator randomGenerator;

	@Override
	public Individual[] select(Individual[] source) {
		int length = source.length;
		Individual[] selected = new Individual[length];

		for (int i = 0; i < length; i++) {
			int pick = randomGenerator.nextInt(length);
			Individual best = source[pick];
			for (int j = 0; j < tournamentSize - 1; j++) {
				pick = randomGenerator.nextInt(length);
				Individual picked = source[pick];
				if (picked.compareTo(best) > 0) {
					best = picked;
				}
			}
			selected[i] = best;
		}

		return selected;
	}

	public int getTournamentSize() {
		return tournamentSize;
	}

	public void setTournamentSize(int tournamentSize) {
		this.tournamentSize = tournamentSize;
	}

	public RandomGenerator getRandomGenerator() {
		return randomGenerator;
	}

	public void setRandomGenerator(RandomGenerator randomGenerator) {
		this.randomGenerator = randomGenerator;
	}

}
